﻿using RBAC.MaHaiBo.Entity.Traceability;
using RBAC.MaHaiBo.Entity;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
using RBAC.MaHaiBo.Repository.Interface.TraceabilityInterface;

namespace RBAC.MaHaiBo.Service.TraceabilityService
{
    /// <summary>
    /// 深加工服务层
    /// </summary>
    public class DeepProcessingService: IDeepProcessingService
    {
        /// <summary>
        /// 日志
        /// </summary>
        ILogger<DeepProcessingService> logger;
        /// <summary>
        /// 深加工
        /// </summary>
        IDeepProcessingRepository DeepProcessingR;
        /// <summary>
        /// 包材
        /// </summary>
        IPackingMaterialsRepository PackingMaterialsR;
        /// <summary>
        /// 添加剂
        /// </summary>
        IAdditiveRepository AdditiveR;
        /// <summary>
        /// 辅材
        /// </summary>
        IAccessoriesRepository AccessoriesR;
        /// <summary>
        /// 原材
        /// </summary>
        IRawMaterialRepository RawMaterialR;
        /// <summary>
        /// 企业
        /// </summary>
        IFirmRepository FirmR;
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="logger">日志</param>
        /// <param name="DeepProcessingR">深加工</param>
        /// <param name="PackingMaterialsR">包材</param>
        /// <param name="AdditiveR">添加剂</param>
        /// <param name="AccessoriesR">辅材</param>
        /// <param name="RawMaterialR">原材</param>
        /// <param name="FirmR">企业</param>
        public DeepProcessingService(ILogger<DeepProcessingService> logger,
        IDeepProcessingRepository DeepProcessingR,
        IPackingMaterialsRepository PackingMaterialsR,
        IAdditiveRepository AdditiveR,
        IAccessoriesRepository AccessoriesR,
        IRawMaterialRepository RawMaterialR,
        IFirmRepository FirmR)
        {
            this.logger = logger;
            this.DeepProcessingR = DeepProcessingR;
            this.PackingMaterialsR = PackingMaterialsR;
            this.AdditiveR = AdditiveR;
            this.AccessoriesR = AccessoriesR;
            this.RawMaterialR = RawMaterialR;
            this.FirmR = FirmR;
        }


        /// <summary>
        /// 添加深加工信息
        /// </summary>
        /// <param name="deep">要添加的深加工信息</param>
        /// <returns>返回受影响行数</returns>
        public  async Task<int> AddDeepProcessing(DeepProcessing deep)
        {
            try
            {
                return await DeepProcessingR.AddAsync(deep);
            }
            catch (Exception ex)
            {
                logger.LogError("【深加工服务层】 添加深加工信息 出错" + ex.Message);
                throw;
            }
        }
        /// <summary>
        /// 修改深加工信息
        /// </summary>
        /// <param name="deep">修改后的深加工信息</param>
        /// <returns>返回受影响行数</returns>
       public async Task<int> updDeepProcessing(DeepProcessing deep)
        {
            try
            {
                return await DeepProcessingR.UpdateAsync(deep);
            }
            catch (Exception ex)
            {
                logger.LogError("【深加工服务层】 修改深加工信息 出错" + ex.Message);
                throw;
            }
        }
        /// <summary>
        /// 显示深加工信息
        /// </summary>
        /// <param name="LOT">批次</param>
        /// <returns>返回深加工信息</returns>
       public async Task<DeepProcessing> DeepProcessingList(string? LOT)
        {
            try
            {
                var list = await DeepProcessingR.GetAsync(p=>p.LOT == LOT);
                var DeepProcessinginfo = new DeepProcessing();
                foreach (var item in list)
                {
                    DeepProcessinginfo = item;
                }
                return DeepProcessinginfo;
            }
            catch (Exception ex)
            {
                logger.LogError("【深加工服务层】 显示深加工信息 出错" + ex.Message);
                throw;
            }
        }

        /// <summary>
        /// 添加原材信息
        /// </summary>
        /// <param name="raw">要添加原材信息</param>
        /// <returns>返回受影响行数</returns>
       public async Task<int> AddRawMaterial(RawMaterial raw)
        {
            try
            {
                return await RawMaterialR.AddAsync(raw); 
            }
            catch (Exception ex)
            {
                logger.LogError("【深加工服务层】 添加原材信息 出错" + ex.Message);
                throw;
            }
        }
        /// <summary>
        /// 修改原材信息
        /// </summary>
        /// <param name="raw">修改后的原材信息</param>
        /// <returns>返回受影响行数</returns>
       public async Task<int> updRawMaterial(RawMaterial raw)
        {
            try
            {
                return await RawMaterialR.UpdateAsync(raw);
            }
            catch (Exception ex)
            {
                logger.LogError("【深加工服务层】 修改原材信息 出错" + ex.Message);
                throw;
            }
        }
        /// <summary>
        /// 显示原料信息
        /// </summary>
        /// <param name="LOT"></param>
        /// <returns>返回原材信息</returns>
       public async Task<RawMaterial> RawMaterialList(string? LOT)
        {
            try
            {
                var list = await RawMaterialR.GetAsync(p => p.LOT == LOT);
                var RawMaterialinfo = new RawMaterial();
                foreach (var item in list)
                {
                    RawMaterialinfo = item;
                }
                return RawMaterialinfo;
            }
            catch (Exception ex)
            {
                logger.LogError("【深加工服务层】 显示原料信息 出错" + ex.Message);
                throw;
            }
        }

        /// <summary>
        /// 添加辅料信息
        /// </summary>
        /// <param name="raw">要添加辅料信息</param>
        /// <returns>返回受影响行数</returns>
       public async Task<int> AddAccessories(Accessories accessories)
        {
            try
            {
                return await AccessoriesR.AddAsync(accessories);
            }
            catch (Exception ex)
            {
                logger.LogError("【深加工服务层】 添加辅料信息 出错" + ex.Message);
                throw;
            }
        }
        /// <summary>
        /// 修改辅料信息
        /// </summary>
        /// <param name="raw">修改后的辅料信息</param>
        /// <returns>返回受影响行数</returns>
       public async Task<int> updAccessories(Accessories accessories)
        {
            try
            {
                return await AccessoriesR.UpdateAsync(accessories);
            }
            catch (Exception ex)
            {
                logger.LogError("【深加工服务层】 修改辅料信息 出错" + ex.Message);
                throw;
            }
        }
        /// <summary>
        /// 显示辅料信息
        /// </summary>
        /// <param name="LOT"></param>
        /// <returns>返回原材信息</returns>
       public async Task<Accessories> AccessoriesList(string? LOT)
        {
            try
            {
                var list = await AccessoriesR.GetAsync(p => p.LOT == LOT);
                var Accessorieslinfo = new Accessories();
                foreach (var item in list)
                {
                    Accessorieslinfo = item;
                }
                return Accessorieslinfo;
            }
            catch (Exception ex)
            {
                logger.LogError("【深加工服务层】 显示辅料信息 出错" + ex.Message);
                throw;
            }
        }

        /// <summary>
        /// 添加添加剂信息
        /// </summary>
        /// <param name="raw">要添加添加剂信息</param>
        /// <returns>返回受影响行数</returns>
       public async Task<int> AddAdditive(Additive additive)
        {
            try
            {
                return await AdditiveR.AddAsync(additive);  
            }
            catch (Exception ex)
            {
                logger.LogError("【深加工服务层】 添加添加剂信息 出错" + ex.Message);
                throw;
            }

        }
        /// <summary>
        /// 修改添加剂信息
        /// </summary>
        /// <param name="raw">修改后的添加剂信息</param>
        /// <returns>返回受影响行数</returns>
       public async Task<int> updAdditive(Additive additive)
        {
            try
            {
                return await AdditiveR.UpdateAsync(additive);
            }
            catch (Exception ex)
            {
                logger.LogError("【深加工服务层】 修改添加剂信息 出错" + ex.Message);
                throw;
            }
        }
        /// <summary>
        /// 显示添加剂信息
        /// </summary>
        /// <param name="LOT"></param>
        /// <returns>返回添加剂信息</returns>
       public async Task<Additive> AdditiveList(string? LOT)
        {
            try
            {
                var list = await AdditiveR.GetAsync(p => p.LOT == LOT);
                var Additivelinfo = new Additive();
                foreach (var item in list)
                {
                    Additivelinfo = item;
                }
                return Additivelinfo;
            }
            catch (Exception ex)
            {
                logger.LogError("【深加工服务层】 显示添加剂信息 出错" + ex.Message);
                throw;
            }
        }

        /// <summary>
        /// 添加包材信息
        /// </summary>
        /// <param name="raw">要添加包材信息</param>
        /// <returns>返回受影响行数</returns>
        public  async Task<int> AddPackingMaterials(PackingMaterials materials)
        {
            try
            {
                return await PackingMaterialsR.AddAsync(materials);
            }
            catch (Exception ex)
            {
                logger.LogError("【深加工服务层】 添加包材信息 出错" + ex.Message);
                throw;
            }
        }
        /// <summary>
        /// 修改包材信息
        /// </summary>
        /// <param name="raw">修改后的包材信息</param>
        /// <returns>返回受影响行数</returns>
       public async Task<int> updPackingMaterials(PackingMaterials materials)
        {
            try
            {
                return await PackingMaterialsR.UpdateAsync(materials);
            }
            catch (Exception ex)
            {
                logger.LogError("【深加工服务层】 修改包材信息 出错" + ex.Message);
                throw;
            }
        }
        /// <summary>
        /// 显示包材信息
        /// </summary>
        /// <param name="LOT"></param>
        /// <returns>返回包材信息</returns>
       public async Task<PackingMaterials> PackingMaterialsList(string? LOT)
        {
            try
            {
                var list = await PackingMaterialsR.GetAsync(p => p.LOT == LOT);
                var PackingMaterialsinfo = new PackingMaterials();
                foreach (var item in list)
                {
                    PackingMaterialsinfo = item;
                }
                return PackingMaterialsinfo;
            }
            catch (Exception ex)
            {
                logger.LogError("【深加工服务层】 显示包材信息 出错" + ex.Message);
                throw;
            }
        }


        /// <summary>
        /// 添加深加工企业信息
        /// </summary>
        /// <param name="firm">企业信息</param>
        /// <returns>返回受影响行数</returns>
      public  async Task<int> AddDeepProcessingFirm(Firm firm)
        {
            try
            {
                var list = await FirmR.GetAsync(p => p.LOT == firm.LOT);
                var firmindo = new Firm();
                foreach (var item in list)
                {
                    firmindo = item;
                }
                firmindo.FirmName = firm.FirmName;
                firmindo.ProductionPermit = firm.ProductionPermit;;
                return await FirmR.UpdateAsync(firmindo);
            }
            catch (Exception ex)
            {
                logger.LogError("【深加工服务层】 添加深加工企业信息 出错" + ex.Message);
                throw;
            }
        }
    }
}
